package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.CollectorUtil;
import com.ecoolex.ying.space.core.dao.GoodsSpecGroupDAO;
import com.ecoolex.ying.space.entity.Goods;
import com.ecoolex.ying.space.entity.GoodsSpecGroup;
import com.ecoolex.ying.space.entity.GoodsSpecGroup.Column;
import com.ecoolex.ying.space.entity.GoodsSpecGroupExample;
import com.ecoolex.ying.space.pojo.EditSpecGroupDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.time.Instant;
import java.util.List;

@Service
public class GoodsSpecGroupManager {
    @Autowired
    private GoodsSpecGroupDAO dao;

    @Autowired
    private GoodsManager goodsManager;
    @Autowired
    private GoodsSpecManager goodsSpecManager;
    @Autowired
    private GoodsProductManager goodsProductManager;

    private void verify(EditSpecGroupDTO group) {
        BizAssert.isTrue(Check.notNullOrTrimEmpty(group.getName()), ResultCode.VERIFY.build(31), "规格分组名称为空");
        BizAssert.isTrue(Check.notNullOrEmpty(group.getSpecList()), ResultCode.VERIFY.build(32), "规格分组下至少需要一个规格值");
    }

    @Transactional
    public void update(Goods goods, final List<EditSpecGroupDTO> specGroup, String updateUser) {
        List<GoodsSpecGroup> oldList = queryByGoodsId(goods.getGoodsId());
        for (int i = 0, size = specGroup.size(); i < size; i++) {
            EditSpecGroupDTO input = specGroup.get(i);
            verify(input);

            if (Check.notNull(input.getSpecGroupId())) {
                GoodsSpecGroup old = oldList.stream().collect(
                        CollectorUtil.first(g -> input.getSpecGroupId().equals(g.getSpecGroupId())));
                BizAssert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(30), "规格分组={}不存在", input.getSpecGroupId());
                // 修改分组信息
                old.setName(input.getName());
                old.setSorting(i + 1);
                update(old, updateUser);

                // 移除
                oldList.remove(old);
                // 更新规格值
                goodsSpecManager.update(old, input.getSpecList(), updateUser);
            } else {
                GoodsSpecGroup group = new GoodsSpecGroup();
                group.setGoodsId(goods.getGoodsId());
                group.setName(input.getName());
                group.setSorting(i + 1);
                create(group, updateUser);

                // 设置输入groupId, 后面规格需要
                input.setSpecGroupId(group.getSpecGroupId());
                // 更新规格值
                goodsSpecManager.update(group, input.getSpecList(), updateUser);
            }
        }
        // 删除多余的规格分组
        oldList.forEach(old -> delete(old, updateUser));

        // 刷新所有规格产品
        goodsProductManager.refresh(goods, specGroup, updateUser);

        // 刷新
        goodsManager.refreshProductPrice(goods.getGoodsId());
    }

    private void update(GoodsSpecGroup group, String updateUser) {
        group.setUpdateUser(updateUser);
        group.setUpdateTime(Date.from(Instant.now()));

        dao.updateByPrimaryKeySelective(group,
                Column.name,
                Column.sorting,
                Column.updateUser,
                Column.updateTime);
    }

    private void create(GoodsSpecGroup group, String createUser) {
        group.setDeleted(GoodsSpecGroup.NOT_DELETED);
        group.setCreateUser(createUser);
        group.setCreateTime(Date.from(Instant.now()));
        group.setUpdateUser(createUser);
        group.setUpdateTime(Date.from(Instant.now()));
        dao.insert(group);
    }

    private void delete(GoodsSpecGroup group, String updateUser) {
        group.setDeleted(GoodsSpecGroup.IS_DELETED);
        group.setUpdateUser(updateUser);
        group.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(group, Column.deleted, Column.updateUser, Column.updateTime);

        // 删除规格值
        goodsSpecManager.deleteByGroupId(group.getSpecGroupId(), updateUser);
    }

    void deleteByGoodsId(Integer goodsId, String updateUser) {
        GoodsSpecGroup record = new GoodsSpecGroup();
        record.setDeleted(GoodsSpecGroup.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        GoodsSpecGroupExample example = new GoodsSpecGroupExample();
        example.createCriteria().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(GoodsSpecGroup.NOT_DELETED);

        dao.updateByExampleSelective(record, example, Column.deleted, Column.updateUser, Column.updateTime);

        List<GoodsSpecGroup> groupList = queryByGoodsId(goodsId);

        groupList.forEach(group -> delete(group, updateUser));
    }

    public List<GoodsSpecGroup> queryByGoodsId(Integer goodsId) {
        GoodsSpecGroupExample example = new GoodsSpecGroupExample();
        example.createCriteria().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(GoodsSpecGroup.NOT_DELETED);
        // 排序
        example.orderBy(Column.sorting.asc());
        return dao.selectByExample(example);
    }
}